import { _decorator, Component, log, Node } from 'cc';
import { Utils } from './CocosUtils';
import { GameConstants, PropType } from '../constants/GameConstants';
import { eventManager } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('Global')
export class Global {

    private static _instance: Global = null;
    private _data: Map<string, any> = new Map();

    // 获取单例实例
    public static getInstance(): Global {
        if (!Global._instance) {
            Global._instance = new Global();
        }
        return Global._instance;
    }

    private constructor() {}
    
    // 当前敌人数量
    public enemyCount: number = 0;

    // 获取道具
    getProp(prop: PropType, unit: boolean): any{
        let amount = Utils.getLocal(prop, 0)
        if(unit){
          return this.converUnit(amount)
        }
        return amount;
    }

    // 增加道具
    addProp(prop: PropType, addNum: number){
        let amount = this.getProp(prop, false);
        amount += addNum
        Utils.saveLocal(prop, amount)
        // 通知刷新道具
        eventManager.emit('ui:prop:refresh',{prop: prop})
    }


     // 货币进位
    converUnit(num: number): string{ 
        console.log(num)
        for (const v of GameConstants.UNITS) {
            let value = num / v[0]
            console.log(value)
            if (value > 0){
                return value.toFixed(1) + v[1]
            }else{
                return num.toString();
            }
        }
    }


}

export const global = Global.getInstance();